// productlist.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    classifyArray: [],//二级分类列表
    productArray: [],//当前分类下，应该显示的商品列表
    selectedClassifyId: '',//选择的分类id
    image: app.globalData.image,
    allwedid: '',
    pageSize: 1
  },
  onLoad: function (options) {
    var that = this
    that.setData({
      allwedid: options.id,
      selectedClassifyId: options.classifyid
    })
    wx.request({
      url: app.globalData.host + "/spuApi/classifySubset",
      data: {
        id: options.id
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          classifyArray: res.data.data
        })
      }
    })
    that.refresh()
  },
  refresh: function () {
    var that = this
    that.setData({
      pageSize: 1,
    })
    that.loadMore()
  },
  loadMore: function () {
    var that = this
    var url = app.globalData.host
    if (!that.data.selectedClassifyId) {
      url = url + '/spuApi/spuClassifyDetail?pageSize=10&pageNo=' + that.data.pageSize + '&id=' + that.data.allwedid + '&type=0'
    } else {
      url = url + '/spuApi/spuProductClassify?id=' + that.data.selectedClassifyId + '&pageNo=' + that.data.pageSize
    }

    wx.showLoading({
      title: '加载中',
    })

    wx.request({
      url: url,
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res)
        var list = that.traverseList(res.data.data)
        list.map(item => {
          if (wx.getStorageSync(that.data.image + item.photoThumb)){
            return item
          }else{
            wx.downloadFile({
              url: that.data.image + item.photoThumb,
              success: function (res) {
                console.log("item.photo:" + item.photo + ",res.temp=" + res.tempFilePath)
                wx.setStorageSync(that.data.image + item.photoThumb, res.tempFilePath)
              }
            })
            return item
          }
        })
        if (list.length > 0) {
          var arr = that.data.productArray;
          if (that.data.pageSize == 1) {
            arr = list
          } else {
            arr = arr.concat(list)
          }
          that.setData({
            pageSize: that.data.pageSize + 1,
            productArray: arr
          })
        } else {
          wx.showToast({
            title: '没有更多了',
            icon: 'success',
            duration: 2000
          })
        }

      }, complete: function (res) {
        setTimeout(function () {
          wx.hideLoading()
        }, 500)
      }
    })

  },
  // 上拉加载数据
  onReachBottom: function () {
    var that = this
    that.loadMore()
  },
  selectClassify: function (e) {
    var that = this
    that.setData({
      selectedClassifyId: e.currentTarget.dataset.id,
      pageSize: 1
    })
    that.refresh()
  },
  // 全部婚纱
  toall: function (options) {
    var that = this
    that.setData({
      selectedClassifyId: ''
    })
    that.refresh()
  },
  traverseList: function (srcList) {
    var that = this
    var currentList = srcList
    for (var index = 0; index < currentList.length; index++) {
      var current = currentList[index];
      current.photo = current.photo.replace("/images/", "/_thumbs/images/")
    }
    return currentList
  },
  // 跳转至婚纱详情页
  towed:function(event){
    var wedid = event.currentTarget.dataset.wedid
    wx.redirectTo({
      url: '../wedproductdetail/wedproductdetail?id=' + wedid,
    })
  }
})